package wps.component.elementsetting {
import mx.events.FlexEvent;

import spark.components.ComboBox;
import spark.events.IndexChangeEvent;

import wps.model.LabelValuePair;
import wps.util.EnableChecker;

public class SettingComboBox extends ComboBox implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _target:Object;

    private var _targetProperty:String;

    private var _targetStyle:String;

    private var _needCopy:Boolean = false;

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function SettingComboBox() {
        super();

        addEventListener(IndexChangeEvent.CHANGE, changeHandler);
        addEventListener(FlexEvent.INITIALIZE, initializeHandler);
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    public function get needCopy():Boolean {
        return _needCopy;
    }

    public function set needCopy(needCopy:Boolean):void {
        _needCopy = needCopy;
    }

    [Bindable]
    public function get target():Object {
        return _target;
    }

    public function set target(target:Object):void {
        _target = target;
        targetChanged();
    }

    public function get targetProperty():String {
        return _targetProperty;
    }

    public function set targetProperty(targetProperty:String):void {
        _targetProperty = targetProperty;
        targetPropertyChanged();
    }

    public function get targetStyle():String {
        return _targetStyle;
    }

    public function set targetStyle(targetStyle:String):void {
        _targetStyle = targetStyle;
        targetStyleChange();
    }

    public function checkEnable():void {
        var checker:EnableChecker = new EnableChecker(this);
        enabled = checker.getEnableValue();
    }

    private function targetChanged():void {
        checkEnable();
        targetStyleChange();
        targetPropertyChanged();

        if (!enabled) {
            selectedIndex = -1;
        }
    }

    private function targetStyleChange():void {
        // image控件也有fontFamliy等文本样式，加入enabled的条件判断
        if (target && targetStyle && enabled) {
            selectedIndex = findSelectedIndex(target.getStyle(targetStyle));
        }
    }

    private function targetPropertyChanged():void {
        if (target && targetProperty && enabled) {
            selectedIndex = findSelectedIndex(target[targetProperty]);
        }
    }

    private function findSelectedIndex(value:Object):int {
        if (dataProvider && dataProvider.length > 0) {
            if (dataProvider.getItemAt(0) is LabelValuePair) {
                // 如果data是LabelValuePair类型
                for (var i:int = 0; i < dataProvider.length; ++i) {
                    var pair:LabelValuePair = dataProvider.getItemAt(i) as LabelValuePair;
                    if (pair.value == value) {
                        return i;
                    }
                }
                return -1;
            } else {
                // 如果data是普通的简单类型，如String, int
                return dataProvider.getItemIndex(value);
            }
        } else {
            return -1;
        }
    }

    private function selectedValue():Object {
        return selectedItem is LabelValuePair ? LabelValuePair(selectedItem).value : selectedItem;
    }

    //--------------------------------------------------------------------------
    //
    // Event handlers
    //
    //--------------------------------------------------------------------------

    protected function changeHandler(e:IndexChangeEvent):void {
        if (target) {
            if (targetStyle) {
                if (targetStyle == "fontFamily") {
                    if (dataProvider.getItemIndex(selectedItem) < 0) {
                        var defaultFont:String = styleManager.getStyleDeclaration("global").getStyle("fontFamily");
                        selectedIndex = dataProvider.getItemIndex(defaultFont);
                    }
                }
                target.setStyle(targetStyle, selectedValue());

            } else if (targetProperty) {
                target[targetProperty] = selectedValue();
            }
        } else {
            selectedIndex = -1;
        }
    }

    private function initializeHandler(e:FlexEvent):void {
        enabled = target ? true : false;
    }

}
}
